package demolinkedlist;

public class MyLinkedList {

    class LinkList {
        public int val;
        public LinkList prev;
        public LinkList next;

        //为什么创建构造体的时候不是这个样子
        /*public LinkList(int val, LinkList prev, LinkList next) {
            this.val = val;
            this.prev = prev;
            this.next = next;
        }*/

        public LinkList(int val) {
            this.val = val;
        }
    }

    public LinkList head;
    public LinkList last;

    // 2、无头双向链表实现

    //头插法
    public void addFirst(int data) {
        LinkList list = new LinkList(data);
        if (head == null) {
            head = last = list;
        } else {
            list.next = head;
            head.prev = list;
            head = list;
        }

    }

    //尾插法
    public void addLast(int data) {
        LinkList list = new LinkList(data);
        if (head == null) {
            head = last = list;
        } else {
            last.next = list;
            list.prev = last;
            last = list;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {//index=4
        LinkList list = new LinkList(data);
        if (index < 0 || index > size()) {
            System.out.println("index越界");
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        LinkList cur = head;

        while (index != 0) {
            cur = cur.next;
            index--;
        }
        cur.prev.next = list;
        list.prev = cur.prev;
        list.next = cur;
        cur.prev = list;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        if (head == null) {
            return false;
        }
        LinkList cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove1(int key) {
        if (head == null) {
            return;
        }
        LinkList cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (key == head.val) {
                    if (head.next != null) {
                        head.next.prev = null;
                        head = head.next;
                    } else {
                        head = last = null;
                    }
                } else {
                    if (key == last.val) {
                        last.prev.next = null;
                        last = last.prev;
                    } else {
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    }
                }
                break;
            } else {
                cur = cur.next;
            }
        }
    }

    public void remove(int key) {
        if (head == null) {
            return;
        }
        LinkList cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    if (head.next != null) {
                        head = head.next;
                        head.prev = null;
                    } else {
                        head = last = null;
                    }
                } else if (cur == last) {
                    last = last.prev;
                    last.next = null;
                } else {
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                }
                break;
            } else {
                cur = cur.next;
            }
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }
        LinkList cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    if (head.next != null) {
                        head = head.next;
                        head.prev = null;
                    } else {
                        head = last = null;
                    }
                } else if (cur == last) {
                    last = last.prev;
                    last.next = null;
                } else {
                    cur.next.prev = cur.prev;
                    cur.prev.next = cur.next;
                }
            }
            cur = cur.next;
        }
    }

    //得到单链表的长度
    public int size() {
        if (head == null) {
            return -1;
        }
        int count = 0;
        LinkList cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        if (head == null) {
            return;
        }
        LinkList cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        if (head == null) {
            return;
        }
        LinkList cur = head;

        while (cur != null) {
            LinkList curN = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curN;
        }
        head = null;
        last = null;
    }

}
